// pages/home/pages/register/register.js
var r = require('../../../../utils/request.js')
let timer
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatar: '',
    name: '',
    sex: '',
    sexName: '请选择性别',

    phone: '',
    code: '',
    start: false,
    count: 60,

    company: '',//单位
    
    provinceName: '',
    provinceId: '',
    cityName: '',
    cityId: '',
    districtName: '',
    districtId: '',
    address: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  // 头像
  chooseImage(e) { //选择图片
    let that = this;
    wx.chooseImage({
      count: 1, // 可上传图片数量
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: res => {
        // console.log(res.tempFilePaths)
        wx.showLoading()
        wx.uploadFile({
          url: app.globalData.dataUrl + '/upload/uploadFile',
          filePath: res.tempFilePaths[0],
          name: 'file',
          header: {
            'content-type': 'multipart/form-data',
            'token': wx.getStorageSync('token')
          },
          success: function (ret) {
            var res = JSON.parse(ret.data)
            wx.hideLoading();
            if (res.retCode == 'SUCCESS') {
              that.setData({
                avatar: res.retData.url
              })
              // console.log(that.data.avatar, '待上传的图片')
            } else {
              wx.showToast({
                title: res.retMsg,
                icon: "none",
                duration: 1500
              })
            }
          },
          fail: function (res) {
            wx.hideLoading();
            wx.showModal({
              title: '错误提示',
              content: '上传图片失败',
              showCancel: false,
              success: function (res) {}
            })
          }
        });
      }
    })
  },


  // 获取值
  getval(e) {
    var that = this
    let type = e.currentTarget.dataset.type

    if (type == 1) {
      this.setData({
        name: e.detail.value
      })
    } else if (type == 2) {
      this.setData({
        phone: e.detail.value
      })
    } else if (type == 3) {
      this.setData({
        code: e.detail.value
      })
    } else if (type == 4) {
      this.setData({
        company: e.detail.value
      })
    } else if (type == 5) {
      this.setData({
        address: e.detail.value
      })
    }
    console.log(e.detail.value)
  },

  // 性别
  getSex() {
    var that = this
    wx.showActionSheet({
      itemList: ['男', '女'],
      success: function (res) {
        // console.log(res.tapIndex)
        if (res.tapIndex == 0) {
          that.setData({
            sexName: '男',
            sex: 'M'
          })
        } else {
          that.setData({
            sexName: '女',
            sex: 'F'
          })
        }
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },

  // 获取验证码
  getCode() {
    let myreg = /^1[3456789]\d{9}$/;
    if (!myreg.test(this.data.phone)) {
      wx.showToast({
        icon: 'none',
        title: '请输入正确手机号码'
      });
      return
    }
    var param = {
      tel: this.data.phone
    }
    r.toPost('/login/senCheckMsg', param, (res) => {
      console.log(res.retData)
      wx.showToast({
        icon: 'none',
        title: res.retMsg
      });
      this.setData({
        start: true
      })
      var num = this.data.count
      timer = setInterval(() => {
        num--;
        this.setData({
          count: num
        });
        if (this.data.count <= 0) {
          clearInterval(timer);
          this.setData({
            start: false
          });
          this.setData({
            count: 60
          })
        }
      }, 1000);
    })
  },

  // 省市区选择
  bindRegionChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.code)
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      provinceName: e.detail.value[0],
      provinceId: e.detail.code[0],
      cityName: e.detail.value[1],
      cityId: e.detail.code[1],
      districtName: e.detail.value[2],
      districtId: e.detail.code[2],
    })
  },

  // 注册
  submit(){
    if(!this.data.avatar){
      wx.showToast({
        icon: 'none',
        title: '请选择头像！'
      });
      return
    }
    if(!this.data.name){
      wx.showToast({
        icon: 'none',
        title: '请输入姓名！'
      });
      return
    }
    if(!this.data.sex){
      wx.showToast({
        icon: 'none',
        title: '请选择性别！'
      });
      return
    }
    let myreg = /^1[3456789]\d{9}$/;
    if (!myreg.test(this.data.phone)) {
      wx.showToast({
        icon: 'none',
        title: '请输入正确手机号码！'
      });
      return
    }
    if(!this.data.code){
      wx.showToast({
        icon: 'none',
        title: '验证码不正确！'
      });
      return
    }
    if(!this.data.company){
      wx.showToast({
        icon: 'none',
        title: '请输入工作单位！'
      });
      return
    }
    if(!this.data.provinceId || !this.data.cityId || !this.data.districtId){
      wx.showToast({
        icon: 'none',
        title: '请选择居住地址！'
      });
      return
    }
    if(!this.data.address){
      wx.showToast({
        icon: 'none',
        title: '请输入详细地址！'
      });
      return
    }

    let param = {
      avatar:this.data.avatar,
      userName:this.data.name,
      sex:this.data.sex,
      phone:this.data.phone,
      msgCode:this.data.code,
      workUnit:this.data.company,
      provinceName:this.data.provinceName,
      provinceId:this.data.provinceId,
      cityName:this.data.cityName,
      cityId:this.data.cityId,
      districtName:this.data.districtName,
      districtId:this.data.districtId,
      address:this.data.address
    }
    r.toPost('/login/register', param, (res) => {
      wx.showToast({
        title: '注册成功！',
        icon:'none'
      })
      setTimeout(() => {
        wx.setStorageSync('token',res.retData.token)
        wx.setStorageSync('userId',res.retData.userId)
        wx.reLaunch({
          url: '/pages/index/index',
        })
      }, 1000);
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})